<!-- 课程章节添加 -->
<template>
  <div>
    <el-dialog title="课程章节" :visible.sync="dialogVisible">
      <div>
        <el-form
          :model="dataForm"
          label-width="80px"
           ref="dataForm"
          :rules="dataRules"
        >
          <el-form-item label="排序" prop="order">
            <el-input
              v-model="dataForm.order"
              placeholder="排序填入比如：1"
            ></el-input>
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="dataForm.title"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    courseInfoId: String,
  },
  data() {
    return {
      dialogVisible: false,
      chapterId: "",
      dataForm: {
        order: "",
        title: "",
        status: 1,
      },
      dataRules: {
        order: [{ required: true, message: "请输入", trigger: "blur" }],
        title: [{ required: true, message: "请输入", trigger: "blur" }],
        status: [{required: true, message:'请输入', trigger: 'blur'}],
      },
    };
  },
  methods: {
    init(id) {
      this.chapterId = id || 0;
      console.log(id)
      this.dialogVisible = true
      this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
      if (id) {
        this.$http({
          url: this.$http.adornUrl(`/teacher/courseManage/getChapter/${id}`),
          method: "get",
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataForm = data.data;
          } else {
            this.$message(data.msg);
          }
        });
      }
    },
    //提交表单
    submit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/teacher/courseManage/${!this.chapterId ? "addChapter" : "updateChapter"}`),
            method: "post",
            data: this.$http.adornData({
                courseInfoId: this.courseInfoId,
                order: this.dataForm.order,
                title: this.dataForm.title,
                status: this.dataForm.status,
                chapterId : this.chapterId
              })  
          }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: "操作成功",
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.dialogVisible = false;
                      this.$emit("refreshDataList");
                    },
                  });
                } else {
                  this.$message(data.msg);
                }
              })
        }
      });
    },
  },
};
</script>

<style  scoped>
</style>
